package main.java.cn.wolfcode.web.controller;
import cn.wolfcode.enums.ConsumptionReportEnum;
import cn.wolfcode.enums.ConsumptionStatusEnum;
import cn.wolfcode.qo.ConsumptionReportQueryObject;
import cn.wolfcode.service.IBusinessService;
import cn.wolfcode.service.IConsumptionReportService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/ConsumptionReport")
public class ConsumptionReportController {
    @Autowired
    private IConsumptionReportService consumptionReportService;
    @Autowired
    private IBusinessService businessService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") ConsumptionReportQueryObject qo){
        model.addAttribute("pageInfo",consumptionReportService.query(qo));
        model.addAttribute("enums", ConsumptionReportEnum.values());
        model.addAttribute("businesses", businessService.listAll());
        return "ConsumptionReport/list";
    }

    @RequestMapping("/listBar")
    public String listBar(Model model, @ModelAttribute("qo") ConsumptionReportQueryObject qo){
       //报表中不需要分组,所以设置分页为0
        qo.setPageSize(0);
        //拿到结果集里面的list集合
        List<HashMap> list = consumptionReportService.query(qo).getList();
        //创建五个集合作为储存数据的容器
        ArrayList<Object> xlist = new ArrayList<>();
        ArrayList<Object> numberlist = new ArrayList<>();
        ArrayList<Object> totalAmountlist = new ArrayList<>();
        ArrayList<Object> payAmountlist = new ArrayList<>();
        ArrayList<Object> discountAmountlist = new ArrayList<>();
        //遍历list集合里面的map,得到每一个map,将map中的key放到不同的list集合中
        for (HashMap map : list) {
            xlist.add(map.get("groupType"));
            numberlist.add(map.get("number"));
            totalAmountlist.add(map.get("totalAmount"));
            payAmountlist.add(map.get("payAmount"));
            discountAmountlist.add(map.get("discountAmount"));
        }
        //将数据共享过去动态生成图表

        model.addAttribute("xlist", JSON.toJSONString(xlist));
        model.addAttribute("numberlist",JSON.toJSONString(numberlist));
        model.addAttribute("totalAmountlist",JSON.toJSONString(totalAmountlist));
        model.addAttribute("payAmountlist",JSON.toJSONString(payAmountlist));
        model.addAttribute("discountAmountlist",JSON.toJSONString(discountAmountlist));
        return "ConsumptionReport/listBar";
    }

}
